local skel = fk.CreateSkill {
  name = "lb__poqiong",
}

Fk:loadTranslationTable {
  ["lb__poqiong"] = "破穹",
  --[":lb__poqiong"] = "若你是上一张牌的目标，你可以横置成为你伤害牌唯一目标的角色及其相邻角色，然后重铸等同于因此横置角色数张牌，获得牌可依次为此牌“助战→目标数+1”、“助战→抵消牌数+1”、“助战→造成火焰伤害”。",
  --9.9
  --[":lb__poqiong"] = "若你是上一张牌的目标，你可以横置成为你伤害牌唯一目标的角色及其相邻角色，然后重铸等同于因此横置角色数张牌，获得牌可依次为此牌“助战→目标数+1”、“助战→抵消牌数+1”、“助战→伤害+1”。若你因此弃置三张获得牌，此牌造成火焰伤害。",
  --10.2
  [":lb__poqiong"] = "若你是上一张牌的目标，你使用伤害牌指定唯一目标后，你可以横置目标及其相邻角色，然后你可以令此牌的至多X个不重复项+1：1.抵消牌数；2.目标数；3.伤害；然后你可以弃置X张牌，令此牌造成火焰伤害（X为因此横置的角色数）。",

  ["#lb__poqiong"] = "破穹：你可以横置 %dest 及其相邻角色",
  --["#lb__poqiong_ask"] = "破穹：请重铸 %arg 张牌",
  --["#lb__poqiong_ask"] = "破穹：你可以令 %arg 的至多 %arg2项数值+1",
  ["@@lb__poqiong"] = "破穹",

  ["@lb__poqiong_zhuzhan-inhand"] = "<font color='yellow'>助战</font>", --助战仅作为意象，此处与真正的助战不同
  ["#lb__poqiong_zhuzhan_1"] = "目标+1",
  ["#lb__poqiong_zhuzhan_2"] = "抵消牌数+1",
  --["#lb__poqiong_zhuzhan_3"] = "造成火焰伤害",
  ["#lb__poqiong_zhuzhan_3"] = "伤害+1",
  ["#lb__poqiong_zhuzhan"] = "<font color='yellow'>助战</font>：你可以弃置一张助战牌，为%arg增加对应效果",
  ["#lb__poqiong_add1"] = "破穹（%arg2/%arg3）：你可以令%arg抵消牌数+1",
  ["#lb__poqiong_add2"] = "破穹（%arg2/%arg3）：你可以为%arg额外指定一个目标",
  ["#lb__poqiong_add3"] = "破穹（%arg2/%arg3）：你可以令%arg伤害+1",
  ["#lb__poqiong_damage"] = "破穹：你可以弃置%arg张牌令%arg2造成火焰伤害",

  ["$lb__poqiong1"] = "剑出，山倾！",
  ["$lb__poqiong2"] = "截云，断岳！",
  ["$lb__poqiong3"] = "上决浮云！",
  ["$lb__poqiong4"] = "下绝地纪！",
}

skel:addEffect(fk.TargetSpecified, {
  audio_index = { 1, 2 },
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and data.firstTarget and data.card.is_damage_card and data:isOnlyTarget(data.to) then
      local useParent = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
      if useParent == nil then return false end
      local lastUseEvent = player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function(e)
        return e.id < useParent.id
      end, 0)[1]
      if lastUseEvent == nil then return false end
      local last_use = lastUseEvent.data
      return last_use.tos and table.contains(last_use.tos, player) and
          table.find({ data.to:getLastAlive(), data.to, data.to:getNextAlive() }, function(to)
            return not to.chained
          end)
    end
    --[[
    return player:hasSkill(skel.name) and target == player and data.card.is_damage_card and data:isOnlyTarget(data.to)
    and player:getMark("@@lb__poqiong") > 0 and
    table.find({data.to:getLastAlive(), data.to, data.to:getNextAlive()}, function (to)
      return not to.chained
    end)
    --]]
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__poqiong::" .. data.to.id })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = 0
    for _, to in ipairs(table.map({ data.to, data.to:getNextAlive(), data.to:getLastAlive() }, function(to)
      return room:getPlayerById(to.id)
    end)) do
      if not to.chained then
        to:setChainState(true)
        if to.chained then
          num = num + 1
        end
      end
    end
    --[[
    if num > 0 then
      local cards = player:getCardIds("he")
      if #cards > num then
        cards = room:askToCards(player, {
          min_num = num,
          max_num = num,
          skill_name = skel.name,
          cancelable = false,
          prompt = "#lb__poqiong_ask:::"..num,
          include_equip = true,
        })
      end
      if #cards > 0 then
        cards = room:recastCard(cards, player, skel.name)
        for i, id in ipairs(cards) do
          local card = Fk:getCardById(id)
          if room:getCardArea(id) == Card.PlayerHand then
            room:setCardMark(card, "@lb__poqiong_zhuzhan-inhand", Fk:translate("#lb__poqiong_zhuzhan_"..i))
          end
        end
        local dis = room:askToDiscard(player, {
          min_num = 1,
          max_num = 3,
          skill_name = "@zhuzhan",
          include_equip = false,
          pattern = tostring(Exppattern {id = cards}),
          prompt = "#lb__poqiong_zhuzhan:::"..data.card:toLogString(),
          skip = true,
        })
        if #dis > 0 then
          local effects = {}
          for _, id in ipairs(dis) do
            local card = Fk:getCardById(id)
            table.insertIfNeed(effects, card:getMark("@lb__poqiong_zhuzhan-inhand"))
          end
          if #dis == 3 then
            data.extra_data = data.extra_data or {}
            data.extra_data.lb__poqiong_zhuzhan = true
            player:broadcastSkillInvoke(skel.name, math.random(3, 4))
          end
          room:throwCard(dis, skel.name, player)
          if table.contains(effects, Fk:translate("#lb__poqiong_zhuzhan_1")) and #data:getExtraTargets() > 0 then
            local to = room:askToChoosePlayers(player, {
              min_num = 1,
              max_num = 1,
              targets = data:getExtraTargets(),
              skill_name = "@zhuzhan",
              prompt = "#variation_addtarget:::"..data.card:toLogString(),
              cancelable = true,
            })
            if #to > 0 then
              data:addTarget(to[1])
            end
          end
          if table.contains(effects, Fk:translate("#lb__poqiong_zhuzhan_2")) then
            data.extra_data = data.extra_data or {}
            data.extra_data.lb__poqiong_response = true
          end
          if table.contains(effects, Fk:translate("#lb__poqiong_zhuzhan_3")) then
            data.use.additionalDamage = (data.use.additionalDamage or 0) + 1    --9.9
          end
        end
        room.logic:getCurrentEvent():addCleaner(function ()
          for _, id in ipairs(cards) do
            local card = Fk:getCardById(id)
            room:setCardMark(card, "@lb__poqiong_zhuzhan-inhand", 0)
          end
        end)
      end
    end
    --]]
    if num > 0 then
      local choice_num = 0
      if room:askToSkillInvoke(player, { skill_name = skel.name,
            prompt = "#lb__poqiong_add1:::" .. data.card:toLogString() .. ":" .. (choice_num + 1) .. ":" .. num, }) then
        data.extra_data = data.extra_data or {}
        data.extra_data.lb__poqiong_response = true
        choice_num = choice_num + 1
      end
      if choice_num < num and #data:getExtraTargets() > 0 then
        local to = room:askToChoosePlayers(player, {
          min_num = 1,
          max_num = 1,
          targets = data:getExtraTargets(),
          skill_name = skel.name,
          prompt = "#lb__poqiong_add2:::" .. data.card:toLogString() .. ":" .. (choice_num + 1) .. ":" .. num,
          cancelable = true,
        })
        if #to > 0 then
          data:addTarget(to[1])
          choice_num = choice_num + 1
        end
      end
      if choice_num < num and room:askToSkillInvoke(player, { skill_name = skel.name,
            prompt = "#lb__poqiong_add3:::" .. data.card:toLogString() .. ":" .. (choice_num + 1) .. ":" .. num, }) then
        data.use.additionalDamage = (data.use.additionalDamage or 0) + 1
      end
      local dis = room:askToDiscard(player, {
        min_num = num,
        max_num = num,
        skill_name = skel.name,
        include_equip = true,
        prompt = "#lb__poqiong_damage:::" .. num .. ":" .. data.card:toLogString(),
        cancelable = true,
      })
      if #dis > 0 then
        data.extra_data = data.extra_data or {}
        data.extra_data.ChangeDamageType = fk.FireDamage
        player:broadcastSkillInvoke(skel.name, math.random(3, 4))
      end
    end
  end,
})
---[[ 宝宝标记
skel:addEffect(fk.CardUsing, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skel.name, true)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@lb__poqiong", (#data.tos > 0 and table.contains(data.tos, player)) and 1 or 0)
  end,
})
--]]

skel:addEffect(fk.TargetConfirmed, {
  can_refresh = function(self, event, target, player, data)
    return target == player and data.extra_data and data.extra_data.lb__poqiong_response
  end,
  on_refresh = function(self, event, target, player, data)
    data:setResponseTimes(data:getResponseTimes(target) + 1, target)
  end,
})

return skel